﻿namespace VirtualTournaments.Business.Financial
{
	public class Account : IAccount
	{
		public const decimal MAX_ALLOWED_DEBIT = 2000m;

		private decimal balance;

		public Account()
		{
			this.balance = 0m;
		}

		public void Credit(decimal ammount)
		{
			this.balance += ammount;
		}

		public void Debit(decimal ammount)
		{
			decimal newValue = this.balance - ammount;
            if (MinFoundsExceded(newValue))
                throw new Business.Financial.InsufficientFundsException();
			this.balance = newValue;
		}

        protected virtual bool MinFoundsExceded(decimal newValue)
        {
            return newValue < Account.MAX_ALLOWED_DEBIT * (-1);
        }

		public decimal Balance
		{
			get { return balance; }
			protected set { balance = value; }
		}

		public void transfer(IAccount to, decimal ammount)
		{
			this.Debit(ammount);
			to.Credit(ammount);
		}
	}
}
